/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MeshlessShapeFunctions;

import java.util.ArrayList;
import java.util.Iterator;

import jmeshless.NewOkCancelDialog;

import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import HashMatrix2.HashMatrix2;
import HashMatrix2.LUFactorization;
import Integration.IFunction1D;
import Integration.derivavty;

/**
 *
 * @author mark
 */
public class JMeshlessInterpolationFunction1D implements IFunction1D{

    protected int PolinomialDegree;
    protected double xii;
    protected double rho;
    protected IKernelFunction func;
    protected HashMatrix2 A;
    protected HashMatrix2 Ax;
    protected HashMatrix2 Axx;
    protected double[] b;
    protected ArrayList<Particle> NodeList;
    protected double EPS = 0.0000001;


    public JMeshlessInterpolationFunction1D(int PolinomialDegree, double xi, double rho, IKernelFunction func) {
        this.PolinomialDegree = PolinomialDegree;
        this.xii = xi;
        this.rho = rho;
        this.func = func;
    }

    public double getRho() {
        return rho;
    }

    public double getXii() {
        return xii;
    }

    public void setNodeList(ArrayList<Particle> NodeList) {
        this.NodeList = NodeList;
    }

    public ArrayList<Double> getBaseVector(double x, double xi){
        ArrayList<Double> L = new ArrayList<Double>();
        for (int i = 0; i <= this.PolinomialDegree; i++) {
            L.add(Math.pow((xi), i));
        }
//        L.add(Math.exp(100*xi));
        return L;
    }

    public ArrayList<Double> getBaseVectorDerivaty(double x, double xi){
        ArrayList<Double> L = new ArrayList<Double>();
        L.add(0.0);
        for (int i = 1; i <= this.PolinomialDegree; i++) {
            L.add(i * Math.pow((xi), i-1));
        }
//        L.add(Math.exp(xi)*100);
        return L;
    }
    
    public ArrayList<Double> getBaseVectorSecondDerivaty(double x, double xi){
        ArrayList<Double> L = new ArrayList<Double>();
        L.add(0.0);
        L.add(0.0);
        for (int i = 2; i <= this.PolinomialDegree; i++) {
            L.add(i * (i-1) * Math.pow((xi), i-2));
        }
        return L;
    }
    
    private boolean isInSupport(Particle p, double x){
    	if (Math.abs(p.getX() - x)<rho) {
			return true;
		} else {
			return false;
		}
    	
    }


    public double calculate(double x){
    	if((xii - rho>x)||(xii + rho<x)){
            return  0;
        }
        ArrayList<Double> BaseVector = getBaseVector(xii, xii);
        int n = BaseVector.size();
        b = new double[n];
        // формуємо матрицю Грамма М
        A = new HashMatrix2(HashMatrix2.ARRAY_TYPE, n);
        for (int i = 0; i < NodeList.size(); i++) {
            Particle particle = NodeList.get(i);
            calculateAlfa(x,particle);
        }    
        
        double val = func.evaluate((x-xii)/rho);
//        BaseVector = getBaseVector(x, new Particle(x, 0.0));
        boolean key=false;
        for (int i = 0; i < b.length; i++) {
            b[i] = BaseVector.get(i)*val;
            if(Math.abs(b[i])>EPS){key = true;}
        }

        double returnValue = 0.0;
        if(key){
            LUFactorization lu = new LUFactorization(A);
            double[] res = lu.calculate(b);

//            BaseVector = getBaseVector(xi.xi, xi);
            BaseVector = getBaseVector(x, x);
            for (int i = 0; i < b.length; i++) {
                returnValue+=BaseVector.get(i)*res[i];
            }
        }
        if (Double.isNaN(returnValue)) {
			return 0;
		}
        return returnValue;
        
    }
   
    public double calculateDerivaty(double x){
        if((xii-rho>x)||(xii+rho<x)){
            return  0;
        }
        ArrayList<Double> BaseVector = getBaseVector(xii, xii);
        int n = BaseVector.size();
        b = new double[n];
        // формуємо матрицю Грамма М
        A = new HashMatrix2(HashMatrix2.ARRAY_TYPE, n);
        for (int i = 0; i < NodeList.size(); i++) {
            Particle particle = NodeList.get(i);
            calculateAlfa(x,particle);
        }    
        
        double val = func.evaluate((x-xii)/rho);
//        BaseVector = getBaseVector(x, new Particle(x, 0.0));
        boolean key=false;
        for (int i = 0; i < b.length; i++) {
            b[i] = BaseVector.get(i)*val;
            if(Math.abs(b[i])>EPS){key = true;}
        }

        double returnValue = 0.0;
        if(key){
            LUFactorization lu = new LUFactorization(A);
            double[] res = lu.calculate(b);
            
//            val = func.evaluateDerivaty((x-xii)/rho);
            key=false;            
            double[] tmp = new double[b.length];
        	for (int j = 0; j < this.NodeList.size(); j++) { // по вузлах
        		Particle xj = NodeList.get(j);
        			val = func.evaluateDerivaty((x - xj.x)/ xj.rho);// func.evaluateDerivaty((x-xj.x)/xj.rho);
        			BaseVector = this.getBaseVector(xj.x, xj.x);
        			double tmpVal = 0.0;
        			for (int k = 0; k < res.length; k++) {// альфа на вектор Р і вагу ...
        				tmpVal += BaseVector.get(k)*res[k];
        			}
        			
        			for (int k = 0; k < res.length; k++) {//число на вектор Р 
        				tmp[k]+=BaseVector.get(k)*tmpVal*val;
        			}
			}
        	val = func.evaluateDerivaty((x - xii)/ rho); //func.evaluateDerivaty((x-xii)/rho);
        	BaseVector = this.getBaseVector(xii, xii);
            for (int i = 0; i < b.length; i++) {
                b[i] = BaseVector.get(i)*val - tmp[i];
                if(Math.abs(b[i])>EPS){key = true;}
            }
            	BaseVector = this.getBaseVectorDerivaty(x, x);
            	for (int j = 0; j < res.length; j++) {
					returnValue+= BaseVector.get(j)*res[j];
				}
            if(key){
            	double[] res2 = lu.calculate(b);
            	
            	BaseVector = this.getBaseVector(x, x);
            	for (int j = 0; j < res2.length; j++) {
					returnValue+= BaseVector.get(j)*res2[j];
				}
            }
            
            
        }
        if (Double.isNaN(returnValue)) {
			return 0;
		}
        return returnValue;
        
    }
    
    public double cal(double x){
    	double h = 10e-3;
    	if((xii-rho>x)||(xii+rho<x)){
            return  0;
        }
        ArrayList<Double> BaseVector = getBaseVector(x, xii);
        int n = BaseVector.size();
        b = new double[n];
        A = new HashMatrix2(HashMatrix2.ARRAY_TYPE, n);
        for (int i = 0; i < NodeList.size(); i++) {
        	Particle particle = NodeList.get(i);
            if (!isInSupport(particle,x)) {
				continue;
			}
            calculateAlfa(x,particle);
        }
        BaseVector = getBaseVector(xii, xii);
        boolean key=false;
        for (int i = 0; i < b.length; i++) {
            b[i] = BaseVector.get(i);
//            if(b[i]!=0.0){key = true;}
            if(Math.abs(b[i])>EPS){key = true;}
        }

        double returnValue = 0.0;
        if(key){
            LUFactorization lu = new LUFactorization(A);
            double[] res = lu.calculate(b);

            double val = func.evaluate((x-xii)/rho);
            BaseVector = getBaseVector(x, x);
            for (int i = 0; i < b.length; i++) {
                returnValue+=BaseVector.get(i)*res[i]*val;
            }
            val = func.evaluate((x-h-xii)/rho);
            BaseVector = getBaseVector(x-h, x-h);
            for (int i = 0; i < b.length; i++) {
                returnValue-=BaseVector.get(i)*res[i]*val;
            }
        }
        return returnValue/h;
        
    }
    
    private void calculateAlfa(double x, Particle xi){
        ArrayList<Double> BaseVector = getBaseVector(x, xi.x);
        int n = BaseVector.size();
        for (int i = 0; i < n; i++) {
            double val = BaseVector.get(i)*func.evaluate((x-xi.x)/xi.rho);
            for (int j = 0; j < n; j++) {
                A.setValue(i, j, A.getValue(i, j)+val*BaseVector.get(j));
            }
        }
    }
    
    
    private void calculateAlfaxx(double x, double xi){
        ArrayList<Double> BaseVector = getBaseVector(x, xi);
        int n = BaseVector.size();
        for (int i = 0; i < n; i++) {
            double val = BaseVector.get(i)*func.evaluateSecondDerivaty((xi-x)/rho);
            for (int j = 0; j < n; j++) {
                Ax.setValue(i, j, Ax.getValue(i, j)+val*BaseVector.get(j));
            }
        }
    }
    

    public double calculateDiffuseDerivaty(double x){
        if( (x>xii+rho) || (x<xii-rho) ){
            return 0.0;
        }
        ArrayList<Double> BaseVector = getBaseVector(x, xii);
        int n = BaseVector.size();
        b = new double[n];
        A = new HashMatrix2(HashMatrix2.ARRAY_TYPE, n);
        for (int i = 0; i < NodeList.size(); i++) {
            Particle particle = NodeList.get(i);
            calculateAlfa(x,particle);
        }

        boolean key=false;
        for (int i = 0; i < b.length; i++) {
            b[i] = BaseVector.get(i)*func.evaluate((xii-x)/rho);
            if(Math.abs(b[i])>EPS){key = true;}
        }

        double returnValue = 0.0;
        if(key){
            LUFactorization lu = new LUFactorization(A);
            double[] res = lu.calculate(b);

            BaseVector = getBaseVectorDerivaty(x, x);

            for (int i = 0; i < b.length; i++) {
                returnValue+=BaseVector.get(i)*res[i];
            }
        }
        if((returnValue + "").equals("NaN")){
            return 0.0;
        }
//        return derivavty.calculate(x, this);
        return returnValue;
    }

    public double calculateSecondDerivaty(double x){
    	if((xii-rho>x)||(xii+rho<x)){
            return  0;
        }
        ArrayList<Double> BaseVector = getBaseVector(x, xii);
        int n = BaseVector.size();
        b = new double[n];
        A = new HashMatrix2(HashMatrix2.ARRAY_TYPE, n);
        for (int i = 0; i < NodeList.size(); i++) {
        	
            Particle particle = NodeList.get(i);
            if (!isInSupport(particle,x)) {
				continue;
			}
            calculateAlfa(x,particle);
        }
        
        double val = func.evaluate((x-xii)/rho);
        BaseVector = getBaseVector(xii, xii);
        boolean key=false;
        for (int i = 0; i < b.length; i++) {
            b[i] = BaseVector.get(i)*val;
            if(Math.abs(b[i])>EPS){key = true;}
        }

        double returnValue = 0.0;
        if(key){
            LUFactorization lu = new LUFactorization(A);
            double[] res = lu.calculate(b);
            
            val = func.evaluateDerivaty((x-xii)/rho);
            key=false;            
            double[] tmp = new double[b.length];
        	for (int j = 0; j < this.NodeList.size(); j++) { // по вузлах
        		Particle xj = NodeList.get(j);
        			val = func.evaluateDerivaty((x-xj.x)/rho);
        			BaseVector = this.getBaseVector(xj.x, xj.x);
        			double tmpVal = 0.0;
        			for (int k = 0; k < res.length; k++) {// альфа на вектор Р і вагу ...
        				tmpVal += BaseVector.get(k)*res[k];
        			}
        			
        			for (int k = 0; k < res.length; k++) {//число на вектор Р 
        				tmp[k]+=BaseVector.get(k)*tmpVal*val;
        			}
			}
        	val = func.evaluateDerivaty((x-xii)/rho);
        	BaseVector = this.getBaseVector(xii, xii);
            for (int i = 0; i < b.length; i++) {
                b[i] = BaseVector.get(i)*val - tmp[i];
                if(Math.abs(b[i])>EPS){key = true;}
            }
            if(key){
            	double[] res2 = lu.calculate(b);
            	
            	//second derivaty of alpha
            	tmp = new double[b.length];
            	for (int j = 0; j < this.NodeList.size(); j++) { // по вузлах
            		Particle xj = NodeList.get(j);
            		val = func.evaluateSecondDerivaty((x-xj.x)/rho);
        			BaseVector = this.getBaseVector(xj.x, xj.x);
        			double tmpVal = 0.0;
        			for (int k = 0; k < res.length; k++) {// альфа на вектор Р і вагу ...
        				tmpVal += BaseVector.get(k)*res[k];
        			}
        			
        			for (int k = 0; k < res.length; k++) {//число на вектор Р 
        				tmp[k]+=BaseVector.get(k)*tmpVal*val;
        			}
        			
        			val = func.evaluateDerivaty((x-xj.x)/rho);
        			BaseVector = this.getBaseVector(xj.x, xj.x);
        			tmpVal = 0.0;
        			for (int k = 0; k < res.length; k++) {// альфа' на вектор Р і вагу ...
        				tmpVal += BaseVector.get(k)*res2[k];
        			}
        			
        			for (int k = 0; k < res.length; k++) {//число на вектор Р 
        				tmp[k]+=BaseVector.get(k)*tmpVal*val;
        			}
    			}
            	val = func.evaluateDerivaty((x-xii)/rho);
            	BaseVector = this.getBaseVector(xii, xii);
                for (int i = 0; i < b.length; i++) {
                    b[i] = BaseVector.get(i)*val - tmp[i];
                    if(Math.abs(b[i])>EPS){key = true;}
                }
                if(key){
                	double[] res3 = lu.calculate(b);
                	BaseVector = this.getBaseVectorSecondDerivaty(x, x);
                	for (int j = 0; j < res2.length; j++) {
    					returnValue+= BaseVector.get(j)*res[j];
    				}
                	BaseVector = this.getBaseVectorDerivaty(x, x);
                	for (int j = 0; j < res2.length; j++) {
    					returnValue+= 2*BaseVector.get(j)*res2[j];
    				}
                	BaseVector = this.getBaseVector(x, x);
                	for (int j = 0; j < res2.length; j++) {
    					returnValue+= BaseVector.get(j)*res3[j];
    				}
                }
            }
        }
        return returnValue;
    }

    public double cSD(double x){
    	double h = 10e-5;
    	if((xii-rho>x)||(xii+rho<x)){
            return  0;
        }
        ArrayList<Double> BaseVector = getBaseVector(x, xii);
        int n = BaseVector.size();
        b = new double[n];
        A = new HashMatrix2(HashMatrix2.ARRAY_TYPE, n);
        for (int i = 0; i < NodeList.size(); i++) {
        	Particle particle = NodeList.get(i);
            if (!isInSupport(particle,x)) {
				continue;
			}
            calculateAlfa(x,particle);
        }
        BaseVector = getBaseVector(xii, xii);
        boolean key=false;
        for (int i = 0; i < b.length; i++) {
            b[i] = BaseVector.get(i);
//            if(b[i]!=0.0){key = true;}
            if(Math.abs(b[i])>EPS){key = true;}
        }

        double returnValue = 0.0;
        if(key){
            LUFactorization lu = new LUFactorization(A);
            double[] res = lu.calculate(b);

            double val = func.evaluate((x-xii)/rho);
            BaseVector = getBaseVector(x, x);
            for (int i = 0; i < b.length; i++) {
                returnValue-=2*BaseVector.get(i)*res[i]*val;
            }
            val = func.evaluate((x-h-xii)/rho);
            BaseVector = getBaseVector(x-h, x-h);
            for (int i = 0; i < b.length; i++) {
                returnValue+=BaseVector.get(i)*res[i]*val;
            }
            val = func.evaluate((x+h-xii)/rho);
            BaseVector = getBaseVector(x+h, x+h);
            for (int i = 0; i < b.length; i++) {
                returnValue+=BaseVector.get(i)*res[i]*val;
            }
        }
        return returnValue/h/h;
        
    }

    
    public static void main(String[] args){

        
        double a = 0.0,b=1;
        double x = a;


        int nn = 10;
        ArrayList<Particle> ls1D = new ArrayList<Particle>();
        ArrayList<JMeshlessInterpolationFunction1D> ls = new ArrayList<JMeshlessInterpolationFunction1D>();
        int PolinomDegree = 1;
        double h = (b-a)/(double)nn;
        double rho = 0.5;
        for (int i = 0; i <=nn; i++) {
            ls1D.add(new Particle(a+h*i, 0, i));
            ls1D.get(ls1D.size()-1).setRho(rho);
        }
        
        
        
        
      
      
//      ls1D = new ArrayList<Particle>();
//      rho = 0.0;
//      
//      ls1D.add( new Particle(0.0 , 0,0 ));
//      rho  = 0.1*2.1;
//      ls1D.get(ls1D.size()-1).setRho(rho);
//      ls1D.add( new Particle(0.05 , 0,1 ));
//      rho  = 0.1*2.1;
//      ls1D.get(ls1D.size()-1).setRho(rho);
//      ls1D.add( new Particle(0.1 , 0,2 ));
//      rho  = 0.15*2.1;
//      ls1D.get(ls1D.size()-1).setRho(rho);
//      ls1D.add( new Particle(0.25 , 0,3 ));
//      rho  = 0.25*2.1;
//      ls1D.get(ls1D.size()-1).setRho(rho);
//      ls1D.add( new Particle(0.5 , 0,4 ));
//      rho  = 0.25*2.1;
//      ls1D.get(ls1D.size()-1).setRho(rho);
//      ls1D.add( new Particle(0.75 , 0,5 ));
//      rho  = 0.25*2.1;
//      ls1D.get(ls1D.size()-1).setRho(rho);
//      ls1D.add( new Particle(0.9, 0,6 ));
//      rho  = 0.15*2.1;
//      ls1D.get(ls1D.size()-1).setRho(rho);
//      ls1D.add( new Particle(0.95 , 0,7 ));
//      rho  = 0.1*2.1;
//      ls1D.get(ls1D.size()-1).setRho(rho);
//      ls1D.add( new Particle(1.0 , 0,8 ));
//      rho  = 0.1*2.1;
//      ls1D.get(ls1D.size()-1).setRho(rho);
      
      
      for (int i = 0; i < ls1D.size(); i++) {
    	  Particle particle = ls1D.get(i);
//    	  double rho = (1.1+0.5*(i+1))*h;
//      rho = (2.1 )*h;
//    	  particle.setRho(rho);
    	  JMeshlessInterpolationFunction1D f = new JMeshlessInterpolationFunction1D(PolinomDegree, particle.x, particle.rho, new JCubicSpline(particle.rho));
    	  ls.add(f);
      }

      for (int i = 0; i < ls1D.size(); i++) {
    	  JMeshlessInterpolationFunction1D f = ls.get(i);
    	  f.setNodeList(ls1D);
      }
      
      
      
      JMeshlessInterpolationFunction1D jmf = ls.get(5);
			double xxx = -1.0;
		 	XYSeries s = new XYSeries("", true, true);
	        while(xxx<=1.0001){
	            double tmp1 = jmf.calculateDerivaty(xxx);
	            tmp1 = xxx - Math.exp(xxx);
	            s.add(xxx,tmp1);
	            xxx+=0.002;
	        }
	        NewOkCancelDialog d = new NewOkCancelDialog(null, false, new XYSeriesCollection(s));
	        d.setLocationRelativeTo(null);
	        d.setVisible(true);
      
      
      
        
//        ls1D.add( new Particle(0.0 , 0,0 ));
//        ls1D.add( new Particle(0.05 , 0,1 ));
//        ls1D.add( new Particle(0.1 , 0,2 ));
//        ls1D.add( new Particle(0.25 , 0,3 ));
//        ls1D.add( new Particle(0.5 , 0,4 ));
//        ls1D.add( new Particle(0.75 , 0,5 ));
//        ls1D.add( new Particle(0.9, 0,6 ));
//        ls1D.add( new Particle(0.95 , 0,7 ));
//        ls1D.add( new Particle(1.0 , 0,8 ));
//
//        
//        
      
//      ls = new ArrayList<JMeshlessInterpolationFunction1D>();
//        double tmpRho = 2.2*Math.max(0, ls1D.get(1).getX()-ls1D.get(0).getX());
////        tmpRho = 1.2*0.3
//        ls1D.get(0).setRho(tmpRho);
//        System.out.println(tmpRho);
////    double tmpRho = 2.33*xz;
//		JMeshlessInterpolationFunction1D tmp = new JMeshlessInterpolationFunction1D(PolinomDegree,ls1D.get(0).getX(),tmpRho,new JGaussian(tmpRho) ); 
//		tmp.setNodeList(ls1D);
//		ls.add(tmp );
//    for (int i = 1; i < ls1D.size()-1; i++) {
//    	tmpRho = 2.2*Math.max(ls1D.get(i).getX()-ls1D.get(i-1).getX(), ls1D.get(i+1).getX()-ls1D.get(i).getX());
//    	System.out.println(tmpRho);
//    	ls1D.get(i).setRho(tmpRho);
//    	tmp = new JMeshlessInterpolationFunction1D(PolinomDegree,ls1D.get(i).getX(),tmpRho,new JGaussian(tmpRho) ); 
//    	tmp.setNodeList(ls1D);
//    	ls.add(tmp );
//    }
//    tmpRho = 2.2*Math.max(0, ls1D.get(ls1D.size()-1).getX()-ls1D.get(ls1D.size()-2).getX());
//    System.out.println(tmpRho);
//    ls1D.get(ls1D.size()-1).setRho(tmpRho);
//	tmp = new JMeshlessInterpolationFunction1D(PolinomDegree,ls1D.get(ls1D.size()-1).getX(),tmpRho,new JGaussian(tmpRho) ); 
//	tmp.setNodeList(ls1D);
//	ls.add(tmp );
//	System.out.println("________________________________________________________________");
	
	XYSeriesCollection col = new XYSeriesCollection();
    for (Iterator<JMeshlessInterpolationFunction1D> it = ls.iterator(); it.hasNext();) {
		JMeshlessInterpolationFunction1D jm = (JMeshlessInterpolationFunction1D) it.next();
		double x1 = 0.0;
		 	XYSeries series = new XYSeries("", true, true);
	        while(x1<=1.0001){
	            double tmp1 = jm.calculateSecondDerivaty(x1);
	            series.add(x1,tmp1);
	            x1+=0.002;
	        }
	        col.addSeries(series);
	}
    NewOkCancelDialog dialog = new NewOkCancelDialog(null, false, col);
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);
    
    col = new XYSeriesCollection();
    for (Iterator<JMeshlessInterpolationFunction1D> it = ls.iterator(); it.hasNext();) {
		JMeshlessInterpolationFunction1D jm = (JMeshlessInterpolationFunction1D) it.next();
		double x1 = 0.0;
		 	XYSeries series = new XYSeries("", true, true);
	        while(x1<=1.0001){
	            double tmp1 = jm.calculate(x1);
	            series.add(x1,tmp1);
	            x1+=0.002;
	        }
	        col.addSeries(series);
	}
    dialog = new NewOkCancelDialog(null, false, col);
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);
    
    double x1 = 0.0;
    while(x1<=1.0001){
        double tmp1 = 0.0;
        for (Iterator<JMeshlessInterpolationFunction1D> it = ls.iterator(); it.hasNext();) {
    		JMeshlessInterpolationFunction1D jm = (JMeshlessInterpolationFunction1D) it.next();
    		tmp1+=jm.calculate(x1);
        }
        
        System.out.println(x1+"\t"+tmp1);
        x1+=0.002;
    }
    
    System.out.println("________________________________________________________________");


        
        XYSeries series1 = new XYSeries("full derivaty calculation", true, true);
        XYSeries series2 = new XYSeries("just function", true, true);
        XYSeries series3 = new XYSeries("differences", true, true);
//        JMeshlessInterpolationFunction1D jMeshlessInterpolationFunction1D = ls.get(5);
//        x= 0.2;
//        System.out.println(x+"\t\t"+(jMeshlessInterpolationFunction1D.calculateDerivaty(x)));
        
        
        
        HashMatrix2 A = new HashMatrix2(HashMatrix2.ARRAY_TYPE, ls.size());
        double[] B = new double[A.getN()];
            for (int i = 0; i < ls.size(); i++) {
            JMeshlessInterpolationFunction1D jMeshlessInterpolationFunction1D = ls.get(i);
                for (int j = 0; j < ls.size(); j++) {
                    x = ls1D.get(j).x;
                    A.setValue(j, i, jMeshlessInterpolationFunction1D.calculate(x));
                }
//            B[i] = ls1D.get(i).x*ls1D.get(i).x;
                B[i] = Math.sin(5*ls1D.get(i).x);
        }

        LUFactorization lu = new LUFactorization(A);
        double[] res = lu.calculate(B);
        
        
        x=a;
         while(x<b+0.0001){
            double tmp2 = 0.0;
            for (int j = 0; j <ls.size(); j++) {
                tmp2+= ls.get(j).calculate(x)*res[j];
            }
            System.out.print(x+"\t"+tmp2+"\n");
            series1.add(x,tmp2);
            x+=0.01;
        }
        
        
//        x=a;
//        
//        System.out.println("f([");
//        while(x<b+0.001){
//        	double y = ls.get(5).calculateDerivaty(x);
//        	series1.add(x,y);
//            System.out.println(x+"\t\t"+(y));
//            x+=0.01;
//        }
//        System.out.println("])");
//        
//        x=a;
//        System.out.println("f([");
//        while(x<b+0.001){
//        	double y = jMeshlessInterpolationFunction1D.calculateSecondDerivaty(x);
//        	series2.add(x,y);
//            System.out.println(x+"\t\t"+(y));
//            x+=0.01;
//        }
//        System.out.println("])");
//        x=a;
//        System.out.println("f([");
//        while(x<b+0.001){
//        	double y = jMeshlessInterpolationFunction1D.calculate(x);
//        	series3.add(x,y);
//            System.out.println(x+"\t\t"+(y));
//            x+=0.01;
//        }
//        System.out.println("])");
//        series1 = null;
        col  = new XYSeriesCollection(series1);
//        col.addSeries(series2);
//        col.addSeries(series1);
        dialog = new NewOkCancelDialog(null, false, col);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);


    }

}